<?php
    include('./config.php');
    include('./request.php');
    $goods_id = $result->goods_id;
    $goods_model = $result->goods_model;
    $market_name = $result->market_name;
    $goods_sendState = $result->$goods_sendState;
    $goods_name = $result->goods_name;
    $user_id = $result->user_id;
    try{
        $pdo = new PDO($dsn,$user,$pass);
        $pdo->beginTransaction();
        $sql = "select goods_num from purchaseorder where goods_id=".$goods_id." and goods_name='".$goods_name."' and goods_model='".$goods_model."' and market_name='".$market_name."'";
        $res = $pdo->prepare($sql);
        $res->execute();
        $rea = $res->fetch(PDO::FETCH_ASSOC);
        $goods_num = $rea['goods_num'];
        if($goods_sendState=="未发货"){
            $sql = "update goodsmodel set goods_num=goods_num+".$goods_num." where goods_id=".$goods_id." and goods_model='".$goods_model."'";
            $res = $pdo->prepare($sql);
            $res->execute();
            if($res){
                $sql = "update goods set goods_num=goods_num+".$goods_num." where goods_id=".$goods_id." and market_name='".$market_name."'";
                $res = $pdo->prepare($sql);
                $res->execute();
                echo json_encode($res);
                if($res){
                   $sql = "delete from purchaseorder where goods_id=".$goods_id." and goods_name='".$goods_name."' and goods_model='".$goods_model."' and market_name='".$market_name."' and user_id=".$user_id;
                   $res = $pdo->prepare($sql);
                   $res->execute();
                   if($res){
                       echo "ok";
                       $pdo->commit();
                   }else{
                       $pdo->rollBack();
                       echo "error";
                   }
                }else{
                    $pdo->rollBack();
                    echo "error";
                }
            }else{
                $pdo->rollBack();
                echo "error";
            }
        }else{
            $sql = "delete from purchaseorder where goods_id=".$goods_id." and goods_name='".$goods_name."' and goods_model='".$goods_model."' and market_name='".$market_name."' and user_id=".$user_id;
            $res = $pdo->prepare($sql);
            $res->execute();
            if($res){
                $pdo->commit();
                echo "ok";
            }else{
                $pdo->rollBack();
                echo "error";
            }
        }
    }catch(PDOException $e){
        echo "error";
    }
?>